package org.pada.client.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author pengshen
 * @date 2023.12.29 18:01
 */
public class MultiResp<T> extends Resp {

    private static final long serialVersionUID = 1L;

    private Collection<T> data;

    public List<T> getData() {
        return null == data ? Collections.emptyList() : new ArrayList<>(data);
    }

    public void setData(Collection<T> data) {
        this.data = data;
    }

    @JsonIgnore
    public boolean isEmpty() {
        return data == null || data.size() == 0;
    }

    @JsonIgnore
    public boolean isNotEmpty() {
        return !isEmpty();
    }

    public static MultiResp buildSuccess() {
        MultiResp resp = new MultiResp();
        resp.setSuccess(true);
        return resp;
    }

    public static MultiResp buildFailure(String errCode, String errMessage) {
        MultiResp resp = new MultiResp();
        resp.setSuccess(false);
        resp.setErrCode(errCode);
        resp.setErrMessage(errMessage);
        return resp;
    }

    public static <T> MultiResp<T> of(Collection<T> data) {
        MultiResp<T> resp = new MultiResp<>();
        resp.setSuccess(true);
        resp.setData(data);
        return resp;
    }

}
